/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog'
], function ($, common, ready, Collection, template, Model, dialog) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            //loading();
            this.fetch();
            this.event();
        },
        fetch: function () {
            var html = template('add-tpl');
            $("#main-view").html(html);
        },
        event: function () {
            this.data = {};
            var _this = this;
            $("#btn-search").on("click", function (e) {
                e.preventDefault();
                _this.select_coupon(e);
            });

            $(".look_platform").on("click", function (e) {
                e.preventDefault();
                _this.select_coupon(e, 1);
            });

            //保存
            $("#btn_save").on("click", function () {
                _this.data.name = $.trim($("#name").val());
                if (_this.data.name == '') {
                    Alert({msg: '请输入名称', type: 'error'});
                    return false;
                }
                _this.data.keyword = $.trim($("#key").val());
                if (_this.data.keyword == '') {
                    Alert({msg: '请输入识别码', type: 'error'});
                    return false;
                }
                var coupon_ids = new Array();
                $(".coupon_id").each(function (i) {
                    if ($(this).val()) {
                        coupon_ids.push($(this).val());
                    }
                });
                if (coupon_ids == '') {
                    Alert({msg: '请选择要增加的优惠券', type: 'error'});
                    return false;
                }
                _this.data.coupon_ids = coupon_ids;
                if (new Model({
                        url: _global.url.api + 'coupon/group',
                        data: _this.data
                    }).post()) {
                    location.href = '/admin/coupon/group';
                }

            });
        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        },
        select_coupon: function (e, is_platform) {
            is_platform = is_platform ? is_platform : 0;
            this.collection = new Collection({
                container: 'data-list',
                template: 'data-items',
                url: _global.url.api + 'coupons',
                condition: {keyword: $("#keyword").val(), status: is_platform == 1 ? 'is_open' : 'is_open_all'},
                model: {
                    event: {
                        '.item-bind:click': 'addcoupon'
                    },
                    remove: function (e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function () {
                                if (model.del()) {
                                    _this.collection.reload();
                                }
                                ;
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    },
                    addcoupon: function (e) {
                        e.preventDefault();
                        var _this = this;
                        var id = _this.data.id;
                        var vals = new Array();
                        $(".coupon_id").each(function () {
                            if ($(this).val()) {
                                vals.push($(this).val());
                            }
                        });
                        var coupon_id = ',' + vals.join(",") + ',';
                        if (coupon_id.indexOf(',' + id + ',') != -1) {
                            Alert({msg: '请不要重复增加同一张优惠券', type: 'error'});
                            return false;
                        }
                        $('#coupon-item').append(template('coupon-item-tpl', {
                            coupon_id: id,
                            site_id: _this.data.site_id,
                            coupon_name: _this.data.name,
                            site_name: _this.data.site_name
                        }));
                        $('.coupon-item-del').on('click', function (e) {
                            e.preventDefault();
                            $(this).parent().parent().remove();
                            $('.item-bind' + id).show();
                            $('.item-bind-success' + id).hide();
                        });
                        $('.item-bind' + id).hide();
                        $('.item-bind-success' + id).show();
                    }

                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        }

    };

    new index();

});